package com.amos.muexamclient.sys.controller;

import com.amos.bean.Result;
import com.amos.muexamclient.framework.service.TokenService;
import com.amos.muexamclient.sys.service.SysUserService;
import com.amos.muexamclient.sys.service.dto.LoginUser;
import com.amos.muexamclient.sys.service.dto.SysUserDTO;
import com.amos.muexamclient.util.SecurityUtils;
import com.amos.muexamcore.util.ServletUtils;
import com.amos.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Copyright © 2018 嘉源锐信. All rights reserved.
 *
 * @Project: mu-exam
 * @ClassName: SysProfileController
 * @Package: com.amos.muexamclient.sys.controller
 * @author: zhuqb
 * @Description:
 * @date: 2020/4/1 0001 下午 13:58
 * @Version: V1.0
 */
@Slf4j
@RestController
@RequestMapping(value = "/sys/profile")
public class SysProfileController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取个人主要信息
     *
     * @return
     */
    @GetMapping
    public Result profile() {
        Map<String, Object> map = new HashMap<>();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUserDTO user = loginUser.getUser();
        map.put("roleGroup", userService.selectUserRoleGroup(user.getId()));
        map.put("postGroup", userService.selectUserPostGroup(user.getId()));
        return Result.success(map);
    }

    @PostMapping
    public Result updateProfile(@RequestBody SysUserDTO user) {
        user.setUpdateTime(new Date());
        try {
            userService.save(user);
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            // 更新缓存用户信息
            loginUser.getUser().setNickName(user.getNickName());
            loginUser.getUser().setPhonenumber(user.getPhonenumber());
            loginUser.getUser().setEmail(user.getEmail());
            loginUser.getUser().setSex(user.getSex());
            tokenService.setLoginUser(loginUser);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error("修改信息异常");
        }
        return Result.ok();
    }

    /**
     * 重置密码
     *
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PutMapping("/update-pwd")
    public Result updatePwd(String oldPassword, String newPassword) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUserDTO user = loginUser.getUser();
        if (!SecurityUtils.matchesPassword(oldPassword, user.getPassword())) {
            return Result.error("修改密码失败，旧密码错误");
        }
        if (Objects.equals(oldPassword, newPassword)) {
            return Result.error("新密码不能与旧密码相同");
        }
        newPassword = SecurityUtils.encryptPassword(newPassword);
        userService.resetPassword(user.getId(), newPassword);
        user.setPassword(newPassword);
        loginUser.setUser(user);
        return Result.ok();
    }

    /**
     * TODO 等待 fastdfs搭建好
     *
     * @param file
     * @return
     */
    @PostMapping("/avatar")
    public Result avatar(@RequestParam("avatarfile") MultipartFile file) {
        if (StringUtils.isNullOrEmpty(file)) {
            return Result.error("上传图片异常");
        }

        return Result.ok();
    }
}
